package br.mikhas.nova;

import java.util.HashMap;
import java.util.Map;

public class CompilationUnit {
	private Map<String, StringEntry> stringTable = new HashMap<String, StringEntry>();
	private Map<String, TypeEntry> typeTable = new HashMap<String, TypeEntry>();
	private Map<String, ClassEntry> classTable = new HashMap<String, ClassEntry>();

	public static final String OBJECT = "Object";

	public CompilationUnit() {
		getType("boolean");
		getType("byte");
		getType("char");
		getType("short");
		getType("int");
		getType("long");
		getType("float");
		getType("double");
		getType(OBJECT);
	}

	public StringEntry getString(String string) {
		StringEntry entry = stringTable.get(string);

		if (entry == null) {
			entry = new StringEntry();
			entry.value = string;
			entry.index = stringTable.size();
			stringTable.put(string, entry);
		}

		return entry;
	}

	public TypeEntry getType(String name) {
		TypeEntry entry = typeTable.get(name);
		if (entry == null) {
			entry = new TypeEntry();
			entry.name = getString(name);
			typeTable.put(name, entry);
		}
		return entry;
	}

	public ClassEntry getClass(String name) {
		ClassEntry entry = classTable.get(name);
		if (entry == null) {
			entry = new ClassEntry();
			entry.type = getType(name);
			entry.parent = getType(OBJECT);
			classTable.put(name, entry);
		}
		return entry;
	}
}
